﻿using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Reflection;
using Z13qu.Model;

namespace Z13qu.Data
{
    public partial class KoalaDbContext : DbContext
    {
        internal KoalaDbContext() : base("KoalaDbContext")
        {
            Database.SetInitializer(new DbContextInitializer());
            //this.Configuration.AutoDetectChangesEnabled = false;
            //this.Configuration.ValidateOnSaveEnabled = false;
            //this.Configuration.LazyLoadingEnabled = false;
            //this.Configuration.ProxyCreationEnabled = false;
        }

        public DbSet<Sys_User> Sys_User { get; set; }
        public DbSet<Sys_UserLogon> Sys_UserLogon { get; set; }
        public DbSet<Sys_Module> Sys_Module { get; set; }
        public DbSet<Sys_ModuleButton> Sys_ModuleButton { get; set; }
        public DbSet<Sys_Role> Sys_Role { get; set; }
        public DbSet<Sys_RoleAuthorize> Sys_RoleAuthorize { get; set; }
        


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            string assembleFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("Z13qu.Data.DLL", "Z13qu.Mapping.DLL").Replace("file:///", "");
            Assembly asm = Assembly.LoadFile(assembleFileName);
            var typesToRegister = asm.GetTypes()
            .Where(type => !String.IsNullOrEmpty(type.Namespace))
            .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }
            base.OnModelCreating(modelBuilder);
        }
    }
}
